﻿@page "/update-setting"
@using System.Reactive.Disposables
@using Blazor.CoffeeTalesApi.Entity
@using Blazor.Core
@using Blazor.Localization
@using Blazor.Pages.UpdateSetting.Modals.ChangeNativeVersion
@using UpdateSettingViewModel = Blazor.ViewModels.UpdateSetting.UpdateSettingViewModel;
@inherits SoraniComponentBase<UpdateSettingViewModel>
@inject IStringLocalizer<UpdateSettingLocalization> Loc

@if (ViewModel is not null)
{
    <Menu Mode="MenuMode.Horizontal"
          OnMenuItemClicked="ChannelClickHandler"
          DefaultSelectedKeys="new []{ViewModel.CurrentChannel.ToString()}">
        @foreach (var c in Enum.GetValues<Channel>().Select(e => e.ToString()))
        {
            <MenuItem Key="@c" Disabled="ViewModel!.IsLoadingChannelInfo">@c</MenuItem>
        }
    </Menu>

    <div class="channel-info">
        @if (ViewModel!.IsLoadingChannelInfo)
        {
            <Spin/>
        }
        else
        {
            <Paragraph>
                <Descriptions Bordered Layout="DescriptionsLayout.Vertical">
                    <DescriptionsItem Title="@Loc[UpdateSettingLocalization.LatestNativeVersion]">
                        @ViewModel.LatestNativeVersion
                        <Button Type="ButtonType.Link" Icon="@IconType.Outline.Edit"
                                Loading="ViewModel.IsChangingNativeVersion"
                                OnClick="() => ViewModel.ChangeNativeVersion.Execute(ChangeNativeVersionOptions.EditType.Latest).Subscribe().DisposeWith(Disposables)"/>
                    </DescriptionsItem>
                    <DescriptionsItem Title="@Loc[UpdateSettingLocalization.MinNativeVersion]">
                        @ViewModel.MinNativeVersion
                        <Button Type="ButtonType.Link" Icon="@IconType.Outline.Edit"
                                Loading="ViewModel.IsChangingNativeVersion"
                                OnClick="() => ViewModel.ChangeNativeVersion.Execute(ChangeNativeVersionOptions.EditType.Min).Subscribe().DisposeWith(Disposables)"/>
                    </DescriptionsItem>
                    <DescriptionsItem Title="@Loc[UpdateSettingLocalization.JumpUrl]">
                        @ViewModel.JumpUrl
                        <Button Type="ButtonType.Link" Icon="@IconType.Outline.Edit"
                                Loading="ViewModel.IsChangingJumpUrl"
                                OnClick="() => ViewModel.ChangeJumpUrl.Execute().Subscribe().DisposeWith(Disposables)"/>
                    </DescriptionsItem>
                </Descriptions>
            </Paragraph>
            <Paragraph>@Loc[UpdateSettingLocalization.HotUpdateSetting]</Paragraph>

            <Settings ViewModel="ViewModel.CurrentSettings"/>
        }
    </div>
}
else
{
    <Empty/>
}